package org.dromara.quick.chat.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.quick.chat.model.bo.query.ImFriendsBo;
import org.dromara.quick.chat.model.bo.query.SettingsFriendsBo;
import org.dromara.quick.chat.service.ImFriendsService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author: Stars
 * @description 针对表【im_friends(好友表（通讯录），记录用户之间的好友关系及相关设置)】的数据库操作Service
 * @Date: 2025/3/6 22:32
 */
@Tag(name = "好友表（通讯录）")
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/ban/record")
public class ImFriendsController extends BaseController {

    private final ImFriendsService imFriendsService;

    /**
     * 通讯录-好友列表
     */
    @Operation(summary = "通讯录-好友列表")
    @GetMapping("/list")
    public R<?> selectFriendsList(ImFriendsBo imFriendsBo, PageQuery pageQuery) {
        return R.ok(imFriendsService.selectFriendsList(imFriendsBo, pageQuery));
    }

    /**
     * 统计临时好友和好友总数量
     *
     */
    @Operation(summary = "统计临时好友和好友总数量")
    @GetMapping("/count")
    public R<?> selectFriendsCount() {
        return R.ok(imFriendsService.selectFriendsCount());
    }

    /**
     * 通讯录-好友详细
     */
    @Operation(summary = "通讯录-好友详细")
    @GetMapping("/detail")
    public R<?> selectFriendsDetail(ImFriendsBo imFriendsBo) {
        return R.ok(imFriendsService.selectFriendsDetail(imFriendsBo));
    }

    /**
     * 聊天框页面查询通讯录信息
     * @param receiverId 对方的id
     * @return
     */
    @Operation(summary = "聊天框页面查询通讯录信息")
    @GetMapping("/chat")
    public R<?> selectFriendsChat(Long receiverId) {
        return R.ok(imFriendsService.selectFriendsChat(receiverId));
    }

    /**
     * 通讯录设置-阅后即焚、聊天记录删除、聊天记录保密、禁止截屏
     */
    @Operation(summary = "通讯录设置-阅后即焚、聊天记录删除、聊天记录保密、禁止截屏")
    @PostMapping("/settings")
    public R<?> settingsFriends(@RequestBody SettingsFriendsBo settingsFriendsBo) {
        if (settingsFriendsBo.getReceiverId() == null) {
            return R.fail("好友ID不能为空");
        }
        imFriendsService.settingsFriends(settingsFriendsBo);
        return R.ok();
    }

    /**
     * 清空聊天记录
     */
    @Operation(summary = "清空聊天记录")
    @PostMapping("/clear")
    public R<?> clearChatRecord(@RequestBody ImFriendsBo imFriendsBo) {

        return R.ok();
    }

    /**
     * 加入黑名单、移除黑名单
     */
    @Operation(summary = "加入黑名单、移除黑名单")
    @GetMapping("/remove/add/blacklist")
    public R<?> removeBlackAddBlacklistFriends(Long receiverId) throws IOException {
        if (receiverId == null) {
            return R.fail("好友ID不能为空");
        }
        imFriendsService.removeBlackAddBlacklistFriends(receiverId);
        return R.ok();
    }


    /**
     * 删除好友
     */
    @Operation(summary = "删除好友")
    @GetMapping("/delete")
    public R<?> deleteFriends(Long receiverId) {
        if (receiverId == null) {
            return R.fail("好友ID不能为空");
        }
        imFriendsService.deleteFriends(receiverId);
        return R.ok();
    }

    /**
     * 发送临时沟通
     */
    @Operation(summary = "发送临时沟通")
    @PostMapping("/send")
    public R<?> sendTemporaryCommunication(@RequestBody ImFriendsBo imFriendsBo) {
        return R.ok();
    }

    /**
     * 发送添加通讯率申请
     */
    @Operation(summary = "发送添加通讯率申请")
    @PostMapping("/apply")
    public R<?> sendAddFriendApply(@RequestBody ImFriendsBo imFriendsBo) {
        return R.ok();
    }

    /**
     * 同意添加通讯录
     */
    @RepeatSubmit
    @Operation(summary = "同意添加通讯录")
    @GetMapping("/agree")
    public R<?> agreeAddFriend(Long friendsId) {
        if (friendsId == null) {
            return R.fail("好友id不能为空");
        }
        Long userId = LoginHelper.getUserId();
        imFriendsService.agreeAddFriend(userId,friendsId);
        return R.ok();
    }

    /**
     * 好友基本信息查询，头像、备注，查询单个
     */
    @GetMapping("/info/base")
    public R<?> selectFriendInfoOne(Long receiverId) {
        if (receiverId == null) {
            return R.fail("参数不能为空");
        }
        return R.ok(imFriendsService.selectFriendInfoOne(receiverId));
    }

    /**
     * 扫码查找好友
     */
    @GetMapping("/search")
    public R<?> searchFriend(String code) {
        if (StringUtils.isEmpty(code)){
            return R.fail("参数不能为空");
        }
        return R.ok(imFriendsService.searchFriend(code));
    }


    /**
     * 底部tab栏-》我的
     * 动态、相册信息
     */
    @GetMapping("/tab")
    public R<?> tabMy() {
        return R.ok(imFriendsService.tabMy());
    }

    /**
     * 用户生成口令
     */
    @GetMapping("/code")
    public R<?> generateCode() {
        return R.ok("",imFriendsService.generateCode());
    }

    /**
     * 根据口令查询用户id
     */
    @GetMapping("/code/{code}")
    public R<?> searchCode(@PathVariable String code) {
        return R.ok("",imFriendsService.searchCode(code));
    }
}

